var core = require('../../core/enocean.js');
var fs = require('fs');
var PATH_CONF = '/../../modules/morning/conf.json';

var getMorningConf = function (req, res) {
	fs.readFile(__dirname + PATH_CONF, 'utf-8', function(error, data) {
		
		if (error) throw error;
		res.end(data);
	});
};

var putMorningConf = function (req, res) {

	var resJson = new Object();
	resJson.coffee_min = req.body.coffee_min;
	resJson.coffee_hour = req.body.coffee_hour;
	resJson.gcalendar_min = req.body.gcalendar_min;
	resJson.gcalendar_hour = req.body.gcalendar_hour;
		
	if (resJson.coffee_min == undefined || resJson.coffee_hour == undefined
		|| resJson.gcalendar_min == undefined || resJson.gcalendar_hour == undefined) {
		// TODO raise exception
		resJson.error = "Paramètres manquants";
		res.end(JSON.stringify (resJson));
	} else {
	
		var resStr = JSON.stringify(resJson);
		fs.writeFile(__dirname + PATH_CONF, resStr, 'utf-8', function (err) {
			if (err) throw err;
			
			res.end(resStr);
		});
	}
};

module.exports.getMorningConf = getMorningConf;
module.exports.putMorningConf = putMorningConf;
